home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.arc / SETCFG.MLS < prev    next >
Encoding:
Text File  |  1988-10-14  |  13.1 KB  |  472 lines

  1. void setcfg()
  2. {
  3. /*
  4.         this subroutine opens the configuration file named
  5.         nbstime.cfg and sets the various global parameters
  6.         based on the contents.  if the file does not exist
  7.         the globals are not altered and the default configuration
  8.         is used.  the global parameters are defined in the main
  9.         program preamble.
  10.     the configuration file is slight different for the different
  11.     versions of the program.  
  12.     the first line always contains the telephone number.
  13.     the second line contains the port specifier at the start.
  14.     for the ibm pc version, this is a single digit or h (for
  15.     a hardware address to be specified on line 6). for the
  16.     UNIX version, if the first two character are letters or
  17.     numbers (nn), they define the port address as /dev/tty<nn>.
  18.     if the first is a letter or number (n) and  the second is - then
  19.     the port address is /dev/tty<n>. if both positions are -, then
  20.     the port is specified by its full path on the last line.
  21. */
  22. #include "nbstime.h"
  23. #include <stdio.h>
  24. #ifdef SUN
  25. #include <sys/file.h>
  26. #include <fcntl.h>     /* note! This extra include was needed for mls at Amoco*/
  27. #endif
  28. #include <ctype.h>
  29. FILE *fopen();
  30. FILE *iop;
  31. int getlst();
  32. extern FILE *jop;
  33. extern int debug;       /* turn on debug mode if = 1 */
  34. #ifdef SUN
  35. char dv[20]; /*device path name*/
  36. #endif
  37. int j,eol,icol;
  38. char c;
  39. char *peof,*perr;
  40. /*
  41.     the IBMPC version can convert the received time
  42.     from UTC to local time using the following two flags.
  43.     the SUN version always sets the time directly in 
  44.     UTC seconds since 1970 since that is the UNIX standard
  45. */
  46. #ifdef IBMPC
  47. extern int utcdif;         /* local time - utc in hours */
  48. extern int dsflag;         /* daylight savings time? 1=yes, 0=no */
  49. #endif
  50. /*
  51.     for the IBMPC version, cmport specifies the comport, 0=com1,
  52.     1=com2, etc.  
  53.     for the SUN version, cmport is the file handle returned when
  54.     the comport is opened at the end of this routine.
  55. */
  56. extern int cmport; 
  57. #ifdef IBMPC
  58. extern int atflag;         /* 1=AT-type machine with CMOS clock, 0= not */
  59. #endif
  60. extern char number[30];    /* number to dial with trailing <cr> and 0 */
  61. extern int echo;           /* expect echoes from modem? 1=yes, 0=no */
  62. extern int hs;             /* 1 = use 1200 baud, 0=use 300 baud     */
  63. #ifdef IBMPC
  64. extern int lpt;             /* 1=  pulse lp on time, 0= do not pulse */
  65. #endif
  66. extern int setclk;         /*  1= set computer clock, 0= do not set */
  67. /*
  68.     wrtdif = 1      compute current time difference and write to file
  69.     wrtdif = 0      do not write to file
  70.     wrtdif = 2      write current difference to file and estimate rate
  71.               using second difference divided by elapsed time
  72. */
  73. extern int wrtdif; 
  74. #ifdef IBMPC
  75. #ifndef BIOS
  76. extern int cmadr;          /* hardware address of port for _d version*/
  77. #endif
  78. #endif
  79.         perr="\n Syntax error in config. file ";
  80.         peof=" -- premature EOF \n ";
  81.         iop=fopen("nbstime.cfg","rt");
  82.         if(iop == 0)
  83.         {
  84.         printf("\n can't open file nbstime.cfg, use default configuration.");
  85.         return;
  86.         }
  87. /*
  88.         first line of file is full telephone number. read it and store
  89.         after ATD which starts command to modem
  90. */
  91.         eol=0;
  92.         for(j=3; (j<29) && (eol==0); j++)
  93.         {
  94.         c=getc(iop);
  95.         switch(c)
  96.            {
  97.            case EOF:
  98.               printf("%s %s",perr,peof);
  99.               abort();
  100.               break;
  101.            case '\n':
  102.               number[j]='\r';        /*terminate with carriage return*/
  103.               number[j+1]=0;        /* and then a null */
  104.               eol=1;
  105.               break;
  106.            default:
  107.               number[j]=c;
  108.               break;
  109.            }
  110.         }
  111.         if(eol == 0)
  112.         {
  113.         printf("%s -- telephone # too long. \n",perr);
  114.         abort();
  115.         }
  116. /*
  117.     for ibm version:
  118.     next line contains comport, 1 = com1, 2 = com2 in col 1, etc.
  119.         if first line contains h, then comport hardware address will be
  120.         specified on sixth line of file.  if comport is specified by
  121.     number, sixth line of file is not used.
  122.         second char sets echo: B=0, anything else =1,
  123.         third character is baud rate, h=1200, l=300;
  124.         fourth character is line printer y=yes, n=no.
  125.         fifth character is set clocks, s=set, d=disable set.
  126.         rest of line is ignored and may be used for a comment
  127.  
  128.     for sun version:
  129.     if first two characters are alphanumeric, they specify port.
  130.     if first character is alphanumeric and second is -, first is port.
  131.     if first is - then port is specified by its full address.
  132.     note that length of port specifier may be 1 or 2 chars so that
  133.     the word next below depends on how the port was specified
  134.     next character is echo as above.
  135.     next is baud rate as above
  136.     next is set/ no set as above.
  137. */
  138.     icol=0;            /* keep track of what col. we are in*/
  139.         c=getc(iop);
  140.     icol++;
  141.         switch (c)
  142.         {
  143.         case EOF:
  144.            printf("%s %s",perr,peof);
  145.            abort();
  146.            break;
  147. #ifdef IBMPC
  148.         case '1':
  149.            cmport=0;
  150.            break;
  151.         case '2':
  152.            cmport=1;
  153.            break;
  154.         #ifndef BIOS
  155.         case '3':
  156.            cmport=2;
  157.            break;
  158.         case '4':
  159.            cmport=3;
  160.            break;
  161.         case 'h':
  162.         case 'H':
  163.            cmport= -1;
  164.            break;
  165.         #endif
  166.         default:
  167.            #ifdef BIOS
  168.            printf("%s -- comport not 1 or 2 \n",perr);
  169.        #endif
  170.        #ifndef BIOS
  171.            printf("%s -- comport must be 1,2 3,4 or h \n",perr);
  172.            #endif
  173.            abort();
  174.            break;
  175. #endif
  176. #ifdef SUN
  177.     case '-':           /* port to be specified by path name */
  178.        cmport= -1;
  179.        break;
  180.     default:
  181.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  182.         {
  183.         printf("%s -- port address must be alphanumeric \n",perr);
  184.         abort();
  185.         break;
  186.         }
  187. /*
  188.     name begins with /dev/tty then add chars just read
  189. */
  190.        sprintf(&dv[0],"%s","/dev/tty");
  191.        dv[8]=c;
  192.        c=getc(iop);
  193.        icol++;
  194.        if(c == '-')             /* comport is only 1 char long*/
  195.         {
  196.         dv[9]='\0';         /*terminate string with null*/
  197.         cmport=1;           /*show port specified ok.   */
  198.         break;
  199.         }
  200.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  201.         {
  202.         printf("%s -- port address must be alphanumeric \n",perr);
  203.         abort();
  204.         break;
  205.         }
  206.        dv[9]=c;            /* get second character of comport*/
  207.        dv[10]='\0';        /* and terminate with null byte */
  208.        cmport= 1;          /* show port specified ok.      */ 
  209.        break;
  210. #endif
  211.         }
  212.         c=getc(iop);
  213.     icol++;
  214.         switch(c)
  215.         {
  216.         case EOF:
  217.            printf("%s %s", perr,peof);
  218.            abort();
  219.            break;
  220.         case 'b':
  221.         case 'B':
  222.            echo=0;
  223.            break;
  224.         case 'e':
  225.         case 'E':
  226.            echo=1;
  227.            break;
  228.         default:
  229.            printf("\nWarning, Line 2 Char %d not e or b -- e assumed.",icol);
  230.            echo=1;
  231.            break;
  232.         }
  233.         c=getc(iop);
  234.     icol++;
  235.         switch (c)
  236.         {
  237.         case EOF:
  238.            printf("%s %s",perr,peof);
  239.            abort();
  240.         case 'h':
  241.         case 'H':
  242.            hs=1;
  243.            break;
  244.         case 'l':
  245.         case 'L':
  246.            hs=0;
  247.            break;
  248.         default:
  249.            printf("\n Warning, Line 2 Char %d not h or l -- h assumed.",icol);
  250.            hs=1;
  251.            break;
  252.         }
  253. #ifdef IBMPC
  254.         c=getc(iop);
  255.     icol++;
  256.         switch (c)
  257.         {
  258.         case EOF:
  259.            printf("%s %s",perr,peof);
  260.            abort();
  261.         case 'y':
  262.         case 'Y':
  263.            lpt=1;
  264.            break;
  265.         case 'n':
  266.         case 'N':
  267.            lpt=0;
  268.            break;
  269.         default:
  270.            printf("\n Warning, Line 2 Char %d not y or n -- n assumed.",icol);
  271.            lpt=0;
  272.            break;
  273.         }
  274. #endif
  275.         c=getc(iop);
  276.     icol++;
  277.         switch (c)
  278.         {
  279.         case EOF:
  280.            printf("%s %s",perr,peof);
  281.            abort();
  282.         case 's':
  283.         case 'S':
  284.            setclk=1;
  285.            break;
  286.         case 'd':
  287.         case 'D':
  288.            setclk=0;
  289.            break;
  290.         default:
  291.            printf("\n Warning, Line 2 Char %d not s or d -- d assumed.",icol);
  292.            setclk=0;
  293.            break;
  294.         }
  295.         c=getc(iop);
  296.     icol++;
  297.         switch (c)
  298.         {
  299.         case EOF:
  300.            printf("%s %s",perr,peof);
  301.            abort();
  302. /*
  303.     the next character specifies if the time difference is to be
  304.     stored in the archive file.  a response of "a" means store difference
  305.     and a response of "n" means do not store it.  any other response is
  306.     taken to be "n" and a diagnostic is printed.  In addition, an upper
  307.     case response "A" also means estimate the rate offset of the clock
  308.     by comparing the last difference with the current difference and
  309.     dividing the second difference by the elapsed time.  this value is
  310.     the rate offset with respect to UTC(NBS).
  311. */
  312.     case 'A':
  313.        if( getlst() == 0)
  314.           {
  315.           printf("\n Can't read previous difference for rate estimate.");
  316.           wrtdif=0;
  317.           }
  318.        else
  319.           {
  320.           wrtdif=2;
  321.           break;
  322.           }
  323.         case 'a':
  324.            wrtdif=1;
  325.            if(  (jop=fopen("nbstime.dif","at")) == 0)
  326.               {
  327.               printf("\n Cannot open file for writing time difference");
  328.               wrtdif=0;
  329.               }
  330.            break;
  331.         case 'n':
  332.         case 'N':
  333.            wrtdif=0;
  334.            break;
  335.         default:
  336.            printf("\n Warning, Line 2 Char %d not a or n -- n assumed.",icol);
  337.            wrtdif=0;
  338.            break;
  339.         }
  340.         while( ( (c=getc(iop)) != '\n') && (c != EOF) );/*skip rest of line*/
  341.         if(c == EOF)
  342.         {
  343.         printf("%s %s",perr,peof);
  344.         abort();
  345.         }
  346. #ifdef IBMPC
  347. /*
  348.         next line contains utcdif as number for hours or symbol for zone
  349.     note that this is not used for SUN since conversion is to UTC
  350.     directly.
  351. */
  352.         c=getc(iop);
  353.         switch (c)
  354.         {
  355.         case 'p':
  356.         case 'P':
  357.            utcdif= -8;
  358.            break;
  359.         case 'm':
  360.         case 'M':
  361.            utcdif= -7;
  362.            break;
  363.         case 'c':
  364.         case 'C':
  365.            utcdif= -6;
  366.            break;
  367.         case 'e':
  368.         case 'E':
  369.            utcdif= -5;
  370.            break;
  371.         case 'z':
  372.         case 'Z':
  373.            utcdif=0;
  374.            break;
  375.         case '-':
  376.         case '+':
  377.         case '0':
  378.         case '1':
  379.         case '2':
  380.         case '3':
  381.         case '4':
  382.         case '5':
  383.         case '6':
  384.         case '7':
  385.         case '8':
  386.         case '9':
  387.            ungetc(c,iop);
  388.            fscanf(iop,"%d",&utcdif);
  389.            break;
  390.         default:
  391.            printf("\n %s -- syntax error in Line 3 -- utc dif.\n",perr);
  392.            abort();
  393.            break;
  394.         }
  395.         while( ( (c=getc(iop) ) != '\n') && (c != EOF));/*skip to next line*/
  396.         if(c == EOF)
  397.         {
  398.         printf("%s %s",perr,peof);
  399.         abort();
  400.         }
  401.         j=fscanf(iop,"%d \n%d",&dsflag,&atflag);
  402.         if(j == EOF)
  403.         {
  404.         printf("%s %s",perr,peof);
  405.         abort();
  406.         }
  407.         if(j != 2)
  408.         {
  409.         printf("%s -- syntax error in AT flag or ds flag \n",perr);
  410.         abort();
  411.         }
  412. #endif
  413. /*
  414.         if the comport is to be specified by its hardware address,
  415.         then the first character on line 2 was 'h' for the IBMPC
  416.     version and '-' for the SUN version -- comport is now -1.
  417.         if so, read the hardware address from the next line.  if comport
  418.         is positive or zero, then next line is ignored.
  419.  
  420.     note that line is read differently depending on which version
  421.     is being generated.  for IBMPC is is read as a hex address
  422.     while for  the SUN it is read as a full path name.
  423.     
  424.     also for the IBMPC the last read was for the AT and ds flags
  425.     and the newline must be skipped at the end.  for the sun
  426.     the last read was for line 2 and the newline at the end
  427.     of line 2 has already been skipped there
  428.  
  429.     also note that the IBMPC code is only compiled if the 
  430.     non-BIOS version is being generated.  The bios version
  431.     does not used the hardware address of the port.
  432. */
  433.         if(cmport < 0)
  434.         {
  435. #ifdef IBMPC
  436. #ifndef BIOS
  437.         while( ( (c=getc(iop)) != '\n') && (c != EOF) ) ;/*skip to next line*/
  438.         if(c == EOF)
  439.            {
  440.            printf("%s %s",perr,peof);
  441.            printf("\n while trying to read comport hardware address.");
  442.            abort();
  443.            }
  444.         j=fscanf(iop,"\n%x",&cmadr);
  445. #endif
  446. #endif
  447. #ifdef SUN
  448.     j=fscanf(iop,"%s",&dv[0]);
  449. #endif
  450.         if(j < 1)
  451.            {
  452.            printf("\n Syntax error reading comport hardware address.");
  453.            abort();
  454.            }
  455.     }  
  456. #ifdef SUN
  457. /*
  458.     now try to open port using path name either read above
  459.     or constructed from template
  460. */
  461.     if(debug != 0) printf("\n port= %s",dv);
  462.     cmport=open(dv, O_RDWR | O_NDELAY);
  463.     if(debug != 0) printf("\n port file descriptor=%d",cmport);
  464.     if(cmport < 0 )
  465.        {
  466.        printf("\n error opening port %s",dv);
  467.        abort();
  468.        }
  469. #endif
  470.         fclose(iop);
  471. }
  472.